package com.siyougame.business.controller.app;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.siyougame.business.controller.admin.user.vo.*;
import com.siyougame.business.controller.app.vo.ThirdUserDTO;
import com.siyougame.business.convert.user.WxUserConvert;
import com.siyougame.business.dal.dataobject.user.WxUserDO;
import com.siyougame.business.service.user.WxUserService;
import com.siyougame.common.annotation.OperateLog;
import com.siyougame.common.pojo.CommonResult;
import com.siyougame.common.pojo.PageResult;
import com.siyougame.common.utils.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.util.Collection;
import java.util.Enumeration;
import java.util.List;

import static com.siyougame.common.enums.OperateTypeEnum.EXPORT;
import static com.siyougame.common.pojo.CommonResult.success;

@Api(tags = "小程序用户")
@RestController
@RequestMapping("/openapi/app/wx-user")
@Validated
@Slf4j
public class AppWxUserController {

    @Resource
    private WxUserService wxUserService;

    @PostMapping("/login")
    @ApiOperation("登录")
    public CommonResult<String> login(@Valid @RequestBody WxUserCreateReqVO createReqVO, HttpServletRequest request) {
        String token = request.getHeader("token");
        String wxUserId = wxUserService.createWxUser(createReqVO, token);
        StpUtil.login(wxUserId);
        return success("登录成功");
    }


    @PostMapping("/test")
    public String test(HttpServletRequest request) {
        try {
            String body = IoUtil.read(request.getInputStream(), StandardCharsets.UTF_8);
            // 遍历请求头
            // Enumeration<String> headerNames = request.getHeaderNames();
            // StringBuilder headers = new StringBuilder("请求头信息:\n");
            // while (headerNames.hasMoreElements()) {
            //     String name = headerNames.nextElement();
            //     String value = request.getHeader(name);
            //     headers.append(name).append(": ").append(value).append("\n");
            // }

            System.out.println("通知内容" + LocalDateTimeUtil.now() + "： " + body);
            //System.out.println(headers);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return "{\"code\":0}";
    }
}
